import sys
import os
import datetime

# 添加daily_checker模块的路径
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'daily_checker')))

from checker import DailyChecker


def run_checker(start_date=None, end_date=None):
    log_directory = r"\\192.168.253.179\firmware_Submit_Test\log\NVR拷机日志"
    performance_log = r'\\192.168.253.179\firmware_Submit_Test\log\NVR拷机日志\性能监控\system_usage.xlsx'
    # 使用本地Excel文件路径
    record_table = os.path.join(os.path.dirname(__file__), r'\\192.168.253.179\firmware_Submit_Test\log\NVR拷机日志\烤机自动记录表.xlsx')
    # record_table = os.path.join(os.path.dirname(__file__), r'烤机自动记录表.xlsx')
    dc = DailyChecker(log_directory, performance_log, record_table)
    
    if start_date and end_date:
        # 解析日期字符串为datetime对象
        start_date_obj = datetime.datetime.strptime(start_date, "%Y-%m-%d")
        end_date_obj = datetime.datetime.strptime(end_date, "%Y-%m-%d")
        
        # 计算日期范围内的天数
        delta = end_date_obj - start_date_obj
        
        # 对日期范围内的每一天执行日志检查
        for i in range(delta.days):
            current_date = start_date_obj + datetime.timedelta(days=i)
            days_before = (datetime.datetime.now() - current_date).days
            if days_before >= 0:  # 确保不是未来日期
                print(f"\n处理日期: {current_date.strftime('%Y-%m-%d')} (距今 {days_before} 天)")
                dc.execute_daily_checks(before_date=days_before)
    else:
        # 默认处理前一天的日志
        dc.execute_daily_checks(before_date=1)
    

if __name__ == '__main__':
    # 从命令行获取参数
    start_date = "2025-05-07"
    end_date = "2025-05-08"
    run_checker(start_date, end_date)
